package com.cxk.hr.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cxk.hr.dao.AttendanceMapper;
import com.cxk.hr.pojo.Attendance;
import com.cxk.hr.pojo.Department;
import com.cxk.hr.pojo.Employee;
import com.cxk.hr.pojo.Overtime;
import com.cxk.hr.service.AttendanceService;

@Service("attendanceServiceImp")
public class AttendanceServiceImpl implements AttendanceService {
	@Resource
	private AttendanceMapper attendanceMapper;

	public List<Attendance> findAll() {
		return attendanceMapper.findAll();
	}

	public List<Overtime> findAllOver() {

		return attendanceMapper.findAllOver();
	}

	public Overtime findByid(Integer employeeNumber) {
		// TODO Auto-generated method stub
		return attendanceMapper.findByid(employeeNumber);
	}

	public Integer updatetime(Overtime overtime) {
		// TODO Auto-generated method stub
		return attendanceMapper.updatetime(overtime);
	}

	public Integer delOver(Integer employeeNumber) {
		// TODO Auto-generated method stub
		return attendanceMapper.delOver(employeeNumber);
	}

	public List<Department> findAllDep() {
		// TODO Auto-generated method stub
		return attendanceMapper.findAllDep();
	}

	public List<Employee> findbydep(Integer departmentnumber) {
		// TODO Auto-generated method stub
		return attendanceMapper.findbydep(departmentnumber);
	}

	public Integer addOverTime(Integer departmentNumber, Integer employeeNumber, Date date) {
		// TODO Auto-generated method stub
		return attendanceMapper.addOverTime(departmentNumber, employeeNumber, date);
	}

	public void addStart(String num) {
		Date newDate = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sf1 = new SimpleDateFormat("HH:mm:ss");
		String starttype = "";
		String conditionstart = "09:00:00";
		String conditionend = "12:00:00";
		String currentYY = sf.format(newDate);
		String currentHH = sf1.format(newDate);
		try {
			Date dateYY = sf.parse(currentYY);
			Date dateamHH = sf1.parse(currentHH);
			Date conditionStart = sf1.parse(conditionstart);
			Date conditionEnd = sf1.parse(conditionend);
			if (dateamHH.after(conditionStart) && dateamHH.before(conditionEnd)) {

			}
			if (dateamHH.before(conditionStart)) {
				starttype = "正常";

			} else {
				starttype = "迟到";
			}
			Attendance attendance = attendanceMapper.findAtten(Integer.parseInt(num), dateYY);
			if (attendance != null) {
				attendanceMapper.updateAtten(Integer.parseInt(num), starttype, dateamHH, dateYY);
			} else {
				attendanceMapper.insert(Integer.parseInt(num), starttype, dateYY, dateamHH);
			}
		} catch (ParseException e) {
		}
	}

	public void addEnd(String num) {
		Date newDate = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sf1 = new SimpleDateFormat("HH:mm:ss");
		String endtype = "";
		String timetype = "";
		String conditions = "18:00:00";
		String conditions1 = "18:30:00";
		String currentYY = sf.format(newDate);
		String currentHH = sf1.format(newDate);
		try {
			Date dataYY = sf.parse(currentYY);
			Date datapmHH = sf1.parse(currentHH);
			Date cond = sf1.parse(conditions);
			Date cond1 = sf1.parse(conditions1);
			if (datapmHH.before(cond)) {
				endtype = "早退";
			} else {
				endtype = "正常";
			}
			if (datapmHH.after(cond1)) {
				timetype = "加班";
			} else {
				timetype = "下午";
			}
			Attendance findAtten = attendanceMapper.findAtten(Integer.parseInt(num), dataYY);
			if (findAtten != null) {
				attendanceMapper.updateendAtten(Integer.parseInt(num), endtype, timetype, datapmHH, dataYY);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

}
